//
//  Plan1ViewController.m
//  VidoarDemo
//
//  Created by xm on 2025/6/17.
//

#import "Plan1ViewController.h"

@interface Plan1ViewController ()

@property (nonatomic, assign) UIBackgroundTaskIdentifier backgrounTask;
@property (nonatomic,strong) NSTimer *backtimer;
@property(nonatomic, assign) NSInteger times;

@property (nonatomic,strong) UILabel *timeLabel;
@property(nonatomic, assign) NSTimeInterval startTimestamp;

@end

@implementation Plan1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.timeLabel];
    // Do any additional setup after loading the view.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self start];
    });
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (UILabel *)timeLabel
{
    if(_timeLabel != nil){
        return _timeLabel;
    }
    
    UILabel* lb = [[UILabel alloc] init];
    lb.top = screenHeight/2 - 100;
    lb.left = 20;
    lb.width = screenWidth - 40;
    lb.height = 50;
    lb.text = @"";
    lb.font = [UIFont systemFontOfSize:50];
    lb.textColor = [UIColor colorWithHexString:@"FF9700"];
    lb.textAlignment = NSTextAlignmentCenter;
    
    _timeLabel = lb;
    
    return _timeLabel;
}

-(void) start
{
    self.startTimestamp = [[NSDate date] timeIntervalSince1970];
    [self backgroundMode];
}



-(void)backgroundMode{
    //创建一个背景任务去和系统请求后台运行的时间
    self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
        self.backgrounTask = UIBackgroundTaskInvalid;
    }];
    
    self.log(@"backgrounTask:%ld", self.backgrounTask);
    
    self.backtimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(applyToSystemForMoreTime) userInfo:nil repeats:YES];
    [self.backtimer fire];
    

}

- (void)applyToSystemForMoreTime {
    NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval diff = timestamp - self.startTimestamp;
    [self updateTip:round(diff)];
    
    self.times ++;
    self.log(@"times:%ld  diff:%lf", self.times, diff);
//    self.log(@"backgroundTimeRemaining%lf", [UIApplication sharedApplication].backgroundTimeRemaining);
   
    if ([UIApplication sharedApplication].backgroundTimeRemaining < 3.0) {//如果剩余时间小于30秒
        self.log(@"restart");
        [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
        self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
            self.backgrounTask = UIBackgroundTaskInvalid;
        }];
    }
    
}
-(void) updateTip:(NSInteger)seconds
{
    NSInteger second = seconds % 60;
    seconds -= second;
    
    NSInteger minute = round((seconds % 3600)/60);
    seconds -= minute*60;
    
    NSInteger hour = round(seconds / 3600);
    
    
    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hour, minute, second];
    
    
}



@end
